﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using StonePonyMVC.StonePony.Services;
using StonePonyMVC.StonePony.Classes;

namespace StonePonyMVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        // Rename word "delete" in routing tables after changing this value.
        public static readonly string DELETE = "delete";

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.IgnoreRoute("{resource}.jpg");
            //routes.IgnoreRoute("{resource}.png");

            /* ***************** Route images ***************** */
            routes.MapRoute(
                "ImagesDefault", 
                "Images/{id}",
                new { controller = "Home", action = "GetImage", path = "images", directory = "", id = "", 
                      pathModifier = "" });

            routes.MapRoute(
                "ImagesShipmentsDelete",
                "Shipments/delete/{directory}/{id}",
                new { controller = "Home", action = "GetImage", path = "shipments", directory = "102", id = "",
                      pathModifier = DELETE});

            routes.MapRoute(
                "ImagesShipments", 
                "Shipments/{directory}/{id}",
                new { controller = "Home", action = "GetImage", path = "shipments", directory = "102", id = "",
                      pathModifier = ""});

            /* ***************** Other routes ***************** */
            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { action = "Index", id = "" }
            );
            
            routes.MapRoute(
              "Root",
              "",
              new { controller = "Home", action = "Index", id = "" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                var mService = new MembershipService();
                StonePonyIdentity identity = mService.BuildStonePonyIdentity(authTicket.Name, true);
                Context.User = new CurrentUser(identity);
            }
        }
    }
}